/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AWT;

import java.awt.Canvas;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @author gurrola
 */
public class EventosMause {

        Frame f = new Frame();
        TextField tfA = new TextField();
        TextField tfB = new TextField();
        Label lA = new Label("X = ");
        Label lB = new Label("Y =");
        Dibujo d = new Dibujo();

        
        public EventosMause(){

        Panel p = new Panel(); 
        p.add(lA);
        p.add(tfA);
        p.add(lB);
        p.add(tfB);
        f.add(p, "South");
        f.add(d);

        //Panel p2 = new Panel();
        //f.add(p2, "North");
        f.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e){
               tfA.setText(e.getX()+"");
               tfB.setText(e.getY()+"");
                e.getY();
            }
        });
       

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }

        });

        d.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt){
                d.addPunto(evt.getX(),evt.getY());
                //  d.paint(null);
            }

        });

        f.setBounds(300, 200, 400, 400);
        f.setVisible(true);

    }

     public class Dibujo extends Canvas {
         private int x1, x2;
         private int y1, y2;
         int click;
         public void paint(Graphics g){
           //  g.drawLine(629, 921, 534, 30);
           if(click == 1){
           //  g.drawOval(x1=-10, y1=-10, 20, 20);
               g.drawLine(x1, y1, x2, y2);
           }else{
             // g.drawOval(x1=-10, y1=-10, 20, 20);
               // g.drawLine(x1, y1, x2, y2);
            }
         }
         public void addPunto(int x, int y){
             click ++;
             if(click==1){
                 this.x1=x;
                 this.y1=y;
             }else{
                 this.x2=x;
                 this.y2=y;
                 click=0;
             }
             repaint();
         }
     }
    public static void main(String [] args){
       EventosMause em = new EventosMause();
         
    }

}